import  { load } from '@tauri-apps/plugin-store'

const defaults = [{
    _id: "like",
    name: '喜欢',
    songs: [
        {
            "album": "浮生",
            "artist": "刘莱斯",
            "audio_url": "https://m801.music.126.net/20250913145904/975881f1814b62f9aebdfd77f5c310a4/jdymusic/obj/wo3DlMOGwrbDjj7DisKw/28481800136/d5c2/7a48/aa41/7a32ca14380aa0c4f08e26cb3b910247.mp3?vuutv=ZUGpqQdrF/dFdRUIZLi9llUqEgeORJxknv1sTlOdpN3Fk5OwuZvb1Zi3jRmkGBeZbbr0n2UHSS7BxYVMbKuWSDe342mi1yZ5qIhsfu6vCo8=",
            "cover": "https://p2.music.126.net/mhWnNBXzrY83dNWJ6GDGwg==/109951163872080254.jpg",
            "id": 445546453,
            "lyrics": "[00:00.00] 作词 : 刘莱斯/陌绪\n[00:01.00] 作曲 : 刘莱斯\n[00:12.97]部分歌词来源网络\n[00:20.93]无人与我把酒分\n[00:29.43]无人告我夜已深\n[00:36.78]无人问我粥可暖\n[00:44.74]无人与我立黄昏\n[00:53.55]他真的很喜欢你 像风走了八千里\n[01:01.49]他真的很喜欢你 像阵雨下到了南极\n[01:09.41]他真的很想念你 像珊瑚沉在海底\n[01:17.57]他真的很喜欢你 不问归期不远万里\n[01:25.71]他真的很喜欢你 像盲人看一出哑剧\n[01:33.59]他真的很喜欢你 像第一首诗不尽人意\n[01:41.52]他真的很喜欢你 像太阳自转无论朝夕\n[01:49.51]他真的很喜欢你 千言万语乐此不疲\n[01:57.33]\n[02:13.40]他真的很喜欢你 像春雨下得淅淅沥沥\n[02:21.33]他真的很喜欢你 像夏日聒噪的蝉鸣\n[02:29.29]他真的很想念你 像秋叶落得悄无声息\n[02:37.15]他真的很喜欢你 像冬天的雪沁在心里\n[02:45.31]他真的很喜欢你 像狗本性难移\n[02:53.10]他真的很喜欢你 所以他可以一直没脸没皮\n[03:01.17]他真的很想念你 无时无刻不在想你\n[03:09.12]他真的很喜欢你 所以他把你捧在手心\n[03:17.15]他真的很喜欢你 所以固执地排比比喻\n[03:25.06]他真的很喜欢你 虽然他的感情实在细腻\n[03:32.99]他真的很想念你 真的无时无刻不在想你\n[03:40.95]他真的很喜欢你 不想浪费时间的一点一滴\n[03:49.01]他真的 很喜欢你\n[03:56.94]他真的 很喜欢你\n[04:04.79]他真的 很想念你\n[04:12.90]他真的 很喜欢你 很喜欢很喜欢你\n[04:23.05]有人与我把酒分\n[04:31.42]有人告我夜已深\n[04:38.87]有人问我粥可暖\n[04:46.72]有人与我立黄昏\n[04:54.91]有人待我诚且真\n[05:03.41]有人忧我细无声\n[05:11.29]有人知我冷与暖\n[05:19.03]有人伴我度余生\n",
            "name": "浮生",
            "tlyric": ""
        },
        {
            "album": "喜欢",
            "artist": "阿肆",
            "audio_url": "https://m801.music.126.net/20250913143531/56328598a1b0a479c82bbeb4f78c1efd/jdymusic/obj/wo3DlMOGwrbDjj7DisKw/62345387370/d44c/8f2d/ecd2/c13bf2f50945601c545cc0d5603ae9cb.mp3?vuutv=qSEjmyY5vwqqzaGM7MaCGg6bkt2t6tN0eE8O7I9tnXa+9tyI0u2wSByzndcEdFXfKclJ8Tw8TcRlED6YjMC9QkOZL94yG1lLpaUnOm6suiA=",
            "cover": "https://p2.music.126.net/K1KWanYhAbACNXXl0GsAoA==/109951168049426473.jpg",
            "id": 526464145,
            "lyrics": "[00:00.000] 作词 : 阿肆\n[00:00.166] 作曲 : 阿肆\n[00:00.332] 编曲 : 姚源/YOCHO\n[00:00.500]扛住了 柴米油盐的麻烦\n[00:04.167]扛住了 朋友聚会的调侃\n[00:07.584]扛住了 世俗生活的刁难\n[00:11.001]却扛不住\n[00:12.735]对你的喜欢\n[00:15.017]\n[00:15.547]亦步亦趋 随你至深不可测\n[00:18.897]患得患失 唯恐你遥不可得\n[00:24.870]却只面不改色\n[00:26.905]为你我认了\n[00:29.054]\n[00:29.304]一朝一夕 使我已固不可彻\n[00:32.604]若即若离 本应是罪不可赦\n[00:38.671]怎不忍心怪你\n[00:40.687]对我太独特\n[00:45.155]无时无刻\n[00:48.504]像是一首\n[00:49.352]无法停止单曲循环的无名歌\n[00:54.731]\n[00:54.935]扛住了\n[00:56.252]柴米油盐的麻烦\n[00:58.986]扛住了\n[00:59.569]朋友聚会的调侃\n[01:02.436]扛住了\n[01:03.018]世俗生活的刁难\n[01:05.852]却扛不住\n[01:07.536]对你的喜欢\n[01:08.835]\n[01:09.305]躲过了\n[01:09.905]世界末日的灾难\n[01:12.691]明白了\n[01:13.337]生命是千金不换\n[01:16.120]躲过了\n[01:16.771]大悲狂喜的极端\n[01:19.537]却躲不过\n[01:21.354]对你的喜欢\n[01:25.486]\n[01:37.941]亦步亦趋 随你至深不可测\n[01:41.208]患得患失 唯恐你遥不可得\n[01:47.208]却只面不改色\n[01:49.208]为你我认了\n[01:51.191]\n[01:51.525]一朝一夕 使我已固不可彻\n[01:54.891]若即若离 本应是罪不可赦\n[02:00.825]怎不忍心怪你\n[02:02.792]对我太独特\n[02:06.974]\n[02:07.308]无时无刻\n[02:10.792]像是一首\n[02:11.625]无法停止单曲循环的无名歌\n[02:17.074]\n[02:17.342]扛住了\n[02:18.474]柴米油盐的麻烦\n[02:21.224]扛住了\n[02:21.824]朋友聚会的调侃\n[02:24.758]扛住了\n[02:25.275]世俗生活的刁难\n[02:28.175]却扛不住\n[02:29.875]对你的喜欢\n[02:31.241]\n[02:31.525]躲过了\n[02:32.275]世界末日的灾难\n[02:34.959]明白了\n[02:35.508]生命是千金不换\n[02:38.508]躲过了\n[02:38.974]大悲狂喜的极端\n[02:41.860]却躲不过\n[02:43.661]对你的喜欢\n[02:45.911]\n[02:46.242]看累了喜新厌旧\n[02:47.912]看累了随波逐流\n[02:49.543]看累了长厢厮守\n[02:51.143]却还没把你看透\n[02:52.992]看破了恩怨情仇\n[02:54.560]看破了蓦然回首\n[02:56.259]看破了从今以后\n[02:58.110]却始终被你左右\n[03:03.594]\n[03:05.162]扛住了\n[03:06.492]柴米油盐的麻烦\n[03:09.227]扛住了\n[03:09.825]朋友聚会的调侃\n[03:12.610]扛住了\n[03:13.293]世俗生活的刁难\n[03:16.075]却扛不住\n[03:17.917]对你的喜欢\n[03:19.376]\n[03:19.610]躲过了\n[03:20.242]世界末日的灾难\n[03:22.976]明白了\n[03:23.610]真心是千金不换\n[03:26.360]躲过了\n[03:26.976]大悲狂喜的极端\n[03:29.776]却躲不过\n[03:31.710]对你的喜欢\n[03:34.345]\n[03:35.161]对你的喜欢\n[03:38.644]对你的喜欢\n[03:41.845]对你的喜欢\n[03:45.311]对你的喜欢\n[03:46.961]\n[03:49.187]对你越喜欢\n[03:52.419]让我越难堪\n[03:55.770]然而再难堪\n[03:59.187]我依然\n[04:00.471]喜欢\n[04:03.486]键盘 : 张伟\n[04:03.919]混音 : YOCHO\n",
            "name": "喜欢",
            "tlyric": ""
        }
    ]
}]
let songsListStore;

load('songs_list', { autoSave: 200, defaults: { "song_bill": defaults }  }).then(store=>{
   return store.clear()
})

// await songsListStore.reset()
const getSongsData = async () => {
    songsListStore = await load('songs_list2', { autoSave: 200, defaults: { "song_bill": defaults }  })
    let res = await songsListStore.get('song_bill')
    return res
}

const setSong = async (data) => {
    let songsData = await getSongsData()
    for (let i = 0; i < songsData.length; i++){
        let newSongs
        if (songsData[i]._id === data._id) {
            let bool = songsData[i].songs.some(item => item.id === data.song.id)
            if(bool){
                newSongs = songsData[i].songs.map(song => {
                    if(song.id === data.song.id){
                        return {
                            _id: crypto.randomUUID(),
                            ...data.song
                        }
                    }

                    return song
                })
            }else{
                newSongs = [data.song, ...songsData[i].songs]
            }
            songsData[i].songs = newSongs
            await songsListStore.set('song_bill', songsData)
            await songsListStore.save()
            break;
        }
        if(!data._id){
            let newsongsData = songsData.map(item=>{
                return {
                    ...item,
                    songs: item.songs.map(song=>{
                        if(song.id === data.song.id){
                            return data.song
                        }
                        return song
                    })
                }
            })
            await songsListStore.set('song_bill', newsongsData)
            await songsListStore.save()
        }
    }
}

const like = async (song) => {
    let songData = await getSongsData()
    for (let i = 0; i < songData.length; i++){
        let songsList = songData[i]
        if(songsList._id === 'like'){
            let bool = songsList.songs.some(item => item.id === song.id)
            if(bool){
                songsList.songs = songsList.songs.filter(item => item.id !== song.id)
            }else{
                songsList.songs.unshift(song)
            }

            break
        }
    }
    await songsListStore.set('song_bill', songData)
    await songsListStore.save()
    return getSongsData()
}
const settingStore = async (params) => {
    let setting = await load('setting', { autoSave: 200 })
    if(params){
       await setting.set('setting', params)
       await setting.save()
    }
    return await setting.get('setting')
}

export {
    songsListStore,
    getSongsData,
    setSong,
    like,
    settingStore
}